<%@page import="empentity.EmpComponent"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="empsession.EmpSessionHome"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="empsession.EmpSessionComponent"%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%!    private EmpSessionComponent empSession = null;
    private String ejbsessionname = "ejb/EmpSessionBean";

    public void jspInit() {
        try {
            InitialContext context = new InitialContext();
            EmpSessionHome empSessionHome = (EmpSessionHome) context.lookup(ejbsessionname);
            empSession = empSessionHome.create();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
%>
<%
    String empno = (String) request.getParameter("id");
    EmpComponent emp = empSession.getEmpByEmpno(new BigDecimal(empno));
    extras.Log log = (extras.Log) session.getAttribute("log");
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="styles.css"/>
        <title>Сотрудник</title>
    </head>
    <body>
        <div class="navigation">
            <%@include file="navigation.jspf"%>
        </div><br/>
        <h1>Просмотр информации о сотруднике</h1>
        <% if (log != null) {%>
        <div class="<%= log.isCorrect() ? "log-correct" : "log-incorrect"%>"><%= log.getMessage()%></div>
        <% } else {%><br/><% }%>
        <br/>
        <table>
            <tr>
                <th colspan="2">Сотрудник</th>
            </tr>
            <tr>
                <th>ФИО</th>
                <td><%= emp.getEname()%></td>
            </tr>
            <tr>
                <th>Дожность</th>
                <td><%= emp.getJob()%></td>
            </tr>
            <tr>
                <th>Менеджер</th>
                <td>
                    <% if (emp.getMgr() != null) {%>
                    <%= empSession.getEmpByEmpno(emp.getMgr()).getEname()%>
                    <% } else {%>
                    <%= "-"%>
                    <% }%>
                </td>
            </tr>
            <tr>
                <th>Дата трудо-<br/>устройства</th>
                <td><%= emp.getHiredate()%></td>
            </tr>
            <tr>
                <th>Зарплата</th>
                <td><%= emp.getSal()%></td>
            </tr>
            <tr>
                <th>Комиссионные</th>
                <td>
                    <% if (emp.getComm() != null) {%>
                    <%= emp.getComm()%>
                    <% } else {%>
                    <%= "-"%>
                    <% }%>
                </td>
            </tr>
            <tr>
                <th>Отдел</th>
                <td><%= empSession.getDeptByDeptno(emp.getDeptno()).getDname()%></td>
            </tr>
        </table>
        <input type="submit" name="edit" value="Редактировать" onclick="self.location='editEmp.jsp?id=<%= emp.getEmpno()%>'">
        <input type="submit" name="delete" value="Удалить" onclick="self.location='viewEmp.jsp?delete=on&id=<%= emp.getEmpno()%>'">
    </body>
    <%
        session.removeAttribute("log");
        try {
            if (request.getParameter("delete") != null) {
                empSession.deleteEmp(emp.getEmpno());
                session.setAttribute("log", new extras.Log(true, "", "Запись удалена"));
                response.sendRedirect("findEmp.jsp");
            }
        } catch (Throwable ex) {
            session.setAttribute("log", new extras.Log(false, "", "Удаление записи невозможно!"));
            response.sendRedirect("findEmp.jsp");
        }
    %>
</html>
